package com.asdc.demo.confirm;

import com.asdc.demo.utils.RabbitMqUtils;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.MessageProperties;

/**
 * @ClassName TaskConfirm01
 * @Author bsy
 * @Description 消息确认
 *
 * 1、队列durable设置成true
 * 2、开启消息确认
 * 3、批量确认
 *
 * 问题: 无法确认单个的是否成功
 * @Date 2022/3/12 16:18
 * @Version 1.0
 */
public class ProdConfirmMul01 {
    private static final String QUEUE_NAME = "QUEUE_CONFIR_VVYX";

    public static void main(String[] args) throws Exception {
        try (Channel channel = RabbitMqUtils.getChannel()) {
            channel.queueDeclare(QUEUE_NAME, true, false, false, null);
            //开启消息确认机制
            channel.confirmSelect();
            long start = System.currentTimeMillis();
            for (int i = 0; i < 100; i++) {
                String msg = "msg" + i;
                channel.basicPublish("", QUEUE_NAME, MessageProperties.PERSISTENT_TEXT_PLAIN, msg.getBytes());
                if (i == 99) {
                    //批量消息确认
                    boolean confirms = channel.waitForConfirms();
                    if (confirms) {
                        System.out.println(msg + "成功发送");
                    }
                }
            }
            long end = System.currentTimeMillis();
            System.out.println("成功向队列发送消息-------用时: " + (end - start) + " ms");
        }
    }
}
